package project.ia04.utils;

import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Random;


public class KnowledgeBaseGenerator {

	public static void createKnowledgeBase() {

		final String filePath = "./KnowledgeBase.foaf";

		try {

			final ArrayList<String> listGameGenre = new ArrayList<String>();
			final ArrayList<String> listGameType = new ArrayList<String>();

			final ArrayList<Integer> listPlayer = new ArrayList<Integer>();
			for (int i = 0; i < Data.NGAMER; i++) {
				listPlayer.add(i);
			}

			for (final String element : Data.listGameGenre) {
				listGameGenre.add(element);
			}
			for (final String element : Data.listGameType) {
				listGameType.add(element);
			}

			final PrintWriter writer = new PrintWriter(new BufferedWriter(
					new FileWriter(filePath)));
			writer.println("@prefix foaf: <http://xmlns.com/foaf/0.1/> .");
			writer.println("@prefix project: <http://project#> .");
			writer.println("@prefix xsd: <http://www.w3.org/2001/XMLSchema#> .");
			writer.println("@prefix ns: <http://example.org/ns#> .");
			writer.println("");

			// Declaration des personnes
			for (int i = 0; i < Data.NGAMER; i++) {
				writer.println("project:" + i + " a foaf:Person.");
				writer.println("project:" + i + " ns:p \"" + i
						+ "\"^^<http://www.w3.org/2001/XMLSchema#integer>.");
			}

			writer.println("");

			final Random random = new Random();

			for (int i = 0; i < Data.NGAMER; i++) {
				final int randomNumberOfGameGenreLoved = random
						.nextInt(listGameGenre.size());
				Collections.shuffle(listGameGenre);
				for (int j = 0; j < randomNumberOfGameGenreLoved; j++) {
					writer.println("project:" + i
							+ " foaf:topic_interest project:"
							+ listGameGenre.get(j) + ".");
				}
				final int randomNumberOfGameTypeLoved = random
						.nextInt(listGameType.size());
				Collections.shuffle(listGameType);
				for (int j = 0; j < randomNumberOfGameTypeLoved; j++) {
					writer.println("project:" + i
							+ " foaf:topic_interest project:"
							+ listGameType.get(j) + ".");
				}
				final int randomNumberOfPeopleKnown = random
						.nextInt(Data.MAXGAMERKNOWN) + 1;
				Collections.shuffle(listPlayer);
				for (int l = 0; l < randomNumberOfPeopleKnown; l++) {
					if (listPlayer.get(l) == i) {
						continue;
					}
					writer.println("project:" + i + " foaf:knows project:"
							+ listPlayer.get(l) + ".");
				}
				writer.println("");
			}
			writer.close();
		} catch (final Exception ex) {
			ex.printStackTrace();
		}
	}
}
